package com.czk.springcloud.controller;

import com.czk.springcloud.entity.CommonResult;
import com.czk.springcloud.entity.Payment;
import com.czk.springcloud.lb.impl.MyLoadBalancer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/12/16 20:26
 */
@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {
    // 单机版时写地址
    // public static final String PAYMENT_URL = "http://localhost:8001";
    /**
     * 集群版本时，写服务名称
     */
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
    @Value("${server.port}")
    private String serverPort;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private MyLoadBalancer loadBalancer;
    @Autowired
    private DiscoveryClient discoveryClient;

    /**
     * 插入实体
     *
     * @param payment
     * @return
     */
    @GetMapping("/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment) {
        // 发送post请求
        return restTemplate.postForObject(PAYMENT_URL + "/payment/insert", payment, CommonResult.class);
    }

    /**
     * 获取实体类
     *
     * @param id
     * @return
     */
    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {
        // 发送get请求
        return restTemplate.getForObject(PAYMENT_URL + "/payment/select/" + id, CommonResult.class);
    }

    /**
     * 测试getForEntity的不同
     *
     * @param id
     * @return
     */
    @GetMapping("/consumer/payments/get/{id}")
    public CommonResult<Payment> getPayment2(@PathVariable("id") Long id) {
        // 发送get请求,拿到ResponseEntity对象
        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL + "/payment/select/" + id, CommonResult.class);
        // 成功
        if (entity.getStatusCode().is2xxSuccessful()) {
            return entity.getBody();
        } else {
            return new CommonResult<>(500, "响应失败");
        }
    }

    @GetMapping("/consumer/lb")
    public String getPaymentLb() {
        // 拿到所有的服务数
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        if (instances == null || instances.size() < 0) {
            return "没有服务";
        }
        // 拿到负载均衡的服务
        ServiceInstance instances1 = loadBalancer.instances(instances);
        // 拿到服务地址
        URI uri = instances1.getUri();
        // 访问
        return restTemplate.getForObject(uri + "/payment/lb", String.class);
    }

    /**
     * zipkin测试
     *
     * @return
     */
    @GetMapping("/consumer/zipkin")
    public String paymentZipkin() {
        String result = restTemplate.getForObject("http://localhost:8001/payment/zipkin", String.class);
        return result;
    }
}
